{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Master Mind LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: masterMind"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #珠玑妙算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>珠玑妙算游戏（the game of master mind）的玩法如下。</p>\n",
    "<p>计算机有4个槽，每个槽放一个球，颜色可能是红色（R）、黄色（Y）、绿色（G）或蓝色（B）。例如，计算机可能有RGGB 4种（槽1为红色，槽2、3为绿色，槽4为蓝色）。作为用户，你试图猜出颜色组合。打个比方，你可能会猜YRGB。要是猜对某个槽的颜色，则算一次“猜中”；要是只猜对颜色但槽位猜错了，则算一次“伪猜中”。注意，“猜中”不能算入“伪猜中”。</p>\n",
    "<p>给定一种颜色组合<code>solution</code>和一个猜测<code>guess</code>，编写一个方法，返回猜中和伪猜中的次数<code>answer</code>，其中<code>answer[0]</code>为猜中的次数，<code>answer[1]</code>为伪猜中的次数。</p>\n",
    "<p><strong>示例：</strong></p>\n",
    "<pre><strong>输入：</strong> solution=\"RGBY\",guess=\"GGRR\"\n",
    "<strong>输出：</strong> [1,1]\n",
    "<strong>解释：</strong> 猜中1次，伪猜中1次。\n",
    "</pre>\n",
    "<p><strong>提示：</strong></p>\n",
    "<ul>\n",
    "<li><code>len(solution) = len(guess) = 4</code></li>\n",
    "<li><code>solution</code>和<code>guess</code>仅包含<code>\"R\"</code>,<code>\"G\"</code>,<code>\"B\"</code>,<code>\"Y\"</code>这4种字符</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [master-mind-lcci](https://leetcode.cn/problems/master-mind-lcci/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [master-mind-lcci](https://leetcode.cn/problems/master-mind-lcci/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"RGRB\"\\n\"BBBY\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        correct = 0\n",
    "        fake_correct = 0\n",
    "        solution = list(solution)\n",
    "        guess = list(guess)\n",
    "        keys = []\n",
    "        for k, (s, g) in enumerate(zip(solution,guess)):\n",
    "            if s == g:\n",
    "                # print(s, g)\n",
    "                keys.append(k - correct)\n",
    "                correct += 1\n",
    "        for k in keys:\n",
    "            solution.pop(k)\n",
    "            guess.pop(k)\n",
    "        # print(solution, guess)\n",
    "        # guess = set(guess)\n",
    "        for g in guess:\n",
    "            if g in solution:\n",
    "                fake_correct += 1\n",
    "                solution.remove(g)\n",
    "        return [correct, fake_correct]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        solution = list(solution)\n",
    "        guess = list(guess)\n",
    "        setGuess = set(guess)\n",
    "        answer = [0, 0]\n",
    "        statistics = dict({})\n",
    "        num = 0\n",
    "        while num < len(solution):\n",
    "            if guess[num] == solution[num]:\n",
    "                answer[0]+=1\n",
    "                del guess[num]\n",
    "                del solution[num]\n",
    "            else:\n",
    "                num+=1\n",
    "        num=0\n",
    "        while num<len(guess):\n",
    "            if guess[num] in solution:\n",
    "                del solution[solution.index(guess[num])]\n",
    "                del guess[num]\n",
    "                answer[1]+=1\n",
    "            else:\n",
    "                num+=1\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        s_map, g_map = Counter(solution), Counter(guess)\n",
    "        guess_right = sum(x==y for x,y in zip(solution,guess))\n",
    "        guess_other = sum(min(s_map[key], g_map[key])  for key in set(solution) & set(guess))\n",
    "        return [guess_right, guess_other-guess_right]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        a = b = 0\n",
    "        cnt = Counter()\n",
    "        for x, y in zip(solution, guess):\n",
    "            if x == y:\n",
    "                a += 1\n",
    "                cnt[x] += 1\n",
    "        cnt1 = Counter(solution)\n",
    "        cnt2 = Counter(guess)\n",
    "        for y, c in cnt2.items():\n",
    "            if y in cnt1:\n",
    "                b += min(cnt1[y], c) - cnt[y]\n",
    "        return [a, b]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        solu, gues = Counter(solution), Counter(guess)\n",
    "        gues_right = sum(x==y for x,y in zip(solution, guess))\n",
    "        guess_all = sum(min(solu[key],gues[key]) for key in set(solution) & set(guess))\n",
    "        return [gues_right, guess_all-gues_right]\n",
    "        # ok = 0\n",
    "        # fank = 0\n",
    "        # for i, col in enumerate(solution):\n",
    "        #     for j, gus in enumerate(guess):\n",
    "        #         if col == gus:\n",
    "        #             if i == j:\n",
    "        #                 ok += 1\n",
    "        #             else:\n",
    "        #                 fank += 1\n",
    "        #                 break\n",
    "        # return [ok, fank]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        r = 0\n",
    "        for i,j in zip(solution,guess):\n",
    "            if i == j:\n",
    "                r +=1 \n",
    "        g = list(guess)\n",
    "        f = 0\n",
    "        for i in solution:\n",
    "            if i in g:\n",
    "                f += 1\n",
    "                g.remove(i)\n",
    "        return [r , f - r]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        hit = 0\n",
    "        hit_other = {}\n",
    "        mock = 0\n",
    "        mock_other = {}\n",
    "        \n",
    "        for i in range(4):\n",
    "            if guess[i] == solution[i]:\n",
    "                hit += 1\n",
    "            else:\n",
    "                hit_other[guess[i]] = 1 + hit_other.get(guess[i], 0)\n",
    "                mock_other[solution[i]] = 1 + mock_other.get(solution[i], 0)\n",
    "        \n",
    "        for c in \"RGBY\":\n",
    "            mock += min(hit_other.get(c, 0), mock_other.get(c, 0))\n",
    "        \n",
    "        return [hit, mock]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        # 猜中指两字符串一一对应，伪猜中指sollution中的字符有几个出现在guess中，减去猜中即可\n",
    "        true, false = 0, 0\n",
    "        ans = list(guess)\n",
    "        # 求解猜中\n",
    "        for i in range(4):\n",
    "            if solution[i]==guess[i]:\n",
    "                true += 1\n",
    "        # 求解伪猜中\n",
    "        for i in range(4):\n",
    "            for j in range(4):\n",
    "                if solution[i]==ans[j]:\n",
    "                    false += 1\n",
    "                    ans[j] = 'a' # 打标记\n",
    "                    break # 推出循环，判断下一个\n",
    "        false -= true\n",
    "        return [true,false]\n",
    "\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        hashMap_s0 = collections.defaultdict(int)\n",
    "        hashMap_g0= collections.defaultdict(int)\n",
    "        hashMap_g1= collections.defaultdict(int)\n",
    "\n",
    "        for i in range(len(solution)):\n",
    "            hashMap_s0[i] = solution[i]\n",
    "        for i in range(len(guess)):\n",
    "            hashMap_g0[i] = guess[i]\n",
    "\n",
    "        for i in range(len(guess)):\n",
    "            hashMap_g1[guess[i]] += 1\n",
    "        \n",
    "        cnt0, cnt1 = 0, 0\n",
    "        for key, val in hashMap_s0.items():\n",
    "            if val == hashMap_g0[key]:\n",
    "                cnt0 += 1\n",
    "        for s in solution:\n",
    "            if hashMap_g1[s] != 0:\n",
    "                cnt1 += 1\n",
    "                hashMap_g1[s] -= 1\n",
    "\n",
    "        return [cnt0, cnt1 - cnt0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        # count1=0\n",
    "        # count2=0\n",
    "        # list1=list(solution)\n",
    "        # list2=list(guess)\n",
    "        # for i in range(len(solution)):\n",
    "        #     if solution[i]==guess[i]:\n",
    "        #         count1=count1+1\n",
    "        #         solution.remove(solution[i])\n",
    "        #         guess.remove(guess[i])\n",
    "        # for j in range(len(guess)):\n",
    "        #     if guess[i] in solution:\n",
    "        #         count2=count2+1\n",
    "        # return [count1,count2]\n",
    "        guess_right=sum(x==y for x,y in zip(solution,guess))\n",
    "        s1,s2=Counter(solution),Counter(guess)\n",
    "        guess_other=sum(min(s1[key],s2[key]) for key in set(solution) & set(guess))\n",
    "        return(guess_right,guess_other-guess_right)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        hash_s = {}\n",
    "        hash_g = {}\n",
    "        num_right = 0\n",
    "        num_right_half = 0\n",
    "        #两个字典记录各个元素出现次数，用来后面进行伪猜对的计算。\n",
    "        for i in range(len(solution)):\n",
    "            hash_s[solution[i]] = hash_s.get(solution[i],0)+1\n",
    "        for i in range(len(guess)):\n",
    "            hash_g[guess[i]] = hash_g.get(guess[i],0)+1\n",
    "        \n",
    "        #算猜对的个数：遍历字符串，如果下标一样，且相同，则为猜对。并且字典里的值要减去1.\n",
    "        for i in range(len(solution)):\n",
    "            if solution[i] == guess[i]:\n",
    "                num_right+=1\n",
    "                hash_s[solution[i]]-=1\n",
    "                hash_g[solution[i]]-=1\n",
    "        \n",
    "        #算伪猜对的个数：两个字典的键 如果有相同的键，取两个字典的值的最小值。代表位置不对，但是有该字母。\n",
    "        \n",
    "        #遍历键\n",
    "        for key in hash_s.keys():\n",
    "            if key in hash_g:\n",
    "                num_right_half+=min(hash_s[key],hash_g[key])\n",
    "        return num_right,num_right_half"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        r = 0\n",
    "        for i, j in zip(solution, guess):\n",
    "            if i == j:\n",
    "                r += 1\n",
    "        z = 0\n",
    "        g = list(guess)\n",
    "        for i in solution:\n",
    "            if i in g:\n",
    "                z += 1\n",
    "                g.remove(i)\n",
    "        return [r, z - r]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        res = [0, 0]\n",
    "        for s, g in zip(solution, guess):\n",
    "            if s == g:\n",
    "                res[0] += 1\n",
    "\n",
    "        rgb = [0, 0, 0, 0]\n",
    "        for c in solution:\n",
    "            if c == \"R\":\n",
    "                rgb[0] += 1\n",
    "            elif c == \"G\":\n",
    "                rgb[1] += 1\n",
    "            elif c == \"B\":\n",
    "                rgb[2] += 1\n",
    "            else:\n",
    "                rgb[3] += 1\n",
    "\n",
    "        rgb2 = [0, 0, 0, 0]\n",
    "        for c in guess:\n",
    "            if c == \"R\":\n",
    "                rgb2[0] += 1\n",
    "            elif c == \"G\":\n",
    "                rgb2[1] += 1\n",
    "            elif c == \"B\":\n",
    "                rgb2[2] += 1\n",
    "            else:\n",
    "                rgb2[3] += 1\n",
    "\n",
    "        for v1, v2 in zip(rgb, rgb2):\n",
    "            res[1] += min(v1, v2)\n",
    "\n",
    "        res[1] -= res[0]\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        a = sum(i == j for i, j in zip(solution, guess))\n",
    "        b = sum((collections.Counter(solution) & collections.Counter(guess)).values())\n",
    "        return [a, b - a]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "\"RGRB\"\n",
    "\"BBBY\"\n",
    "\"RGBY\"\n",
    "\"GGRR\"\n",
    "\n",
    "len(solution) = len(guess) = 4\n",
    "There are only \"R\",\"G\",\"B\",\"Y\" in solution and guess.\n",
    "\n",
    "Hash Table, String, Counting\n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        n = len(solution)\n",
    "        mp = { 'R':0, 'G':1, 'B':2, 'Y':3 }\n",
    "        cnter = [0]*4\n",
    "        for i in range(n):\n",
    "            cnter[mp[solution[i]]] += 1\n",
    "        hits, totalHits = 0, 0\n",
    "        for i in range(n):\n",
    "            if solution[i] == guess[i]: hits += 1\n",
    "            if cnter[mp[guess[i]]] > 0:\n",
    "                totalHits += 1\n",
    "                cnter[mp[guess[i]]] -= 1\n",
    "        return [hits, totalHits - hits]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        temp = defaultdict(int)\n",
    "        correct = 0\n",
    "        fake_correct = 0\n",
    "        for i in range(len(solution)):\n",
    "            if solution[i] == guess[i]:\n",
    "                correct += 1\n",
    "            else:\n",
    "                if temp[solution[i]] < 0:\n",
    "                    fake_correct += 1\n",
    "                if temp[guess[i]] > 0:\n",
    "                    fake_correct += 1\n",
    "                temp[solution[i]] += 1\n",
    "                temp[guess[i]] -= 1\n",
    "        return [correct, fake_correct]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        ans = [0,0]\n",
    "        val = collections.defaultdict(int)\n",
    "        valguess = \"\"\n",
    "        for i in range(4):\n",
    "            if solution[i]==guess[i]:\n",
    "                ans[0]+=1\n",
    "            else:\n",
    "                val[solution[i]]+=1\n",
    "                valguess+=guess[i]\n",
    "        for c in valguess:\n",
    "            if val[c]>0:\n",
    "                val[c]-=1\n",
    "                ans[1]+=1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        a = b = 0\n",
    "        adict = defaultdict(int)\n",
    "        bdict = defaultdict(int)\n",
    "        for s, g in zip(solution, guess):\n",
    "            if s == g:\n",
    "                a += 1\n",
    "            else:\n",
    "                adict[s] += 1\n",
    "                bdict[g] += 1\n",
    "        for k in bdict:\n",
    "            b += min(adict[k], bdict[k])\n",
    "        return [a, b]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        s_map,g_map=Counter(solution),Counter(guess)\n",
    "        guess_right=sum(x==y for x,y in zip(solution,guess))\n",
    "        guess_other=sum(min(s_map[key],g_map[key]) for key in set(solution) & set(guess))\n",
    "        return [guess_right,guess_other-guess_right]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        a0 = 0\n",
    "        a1 = 0\n",
    "        d ={}\n",
    "        for i in solution:\n",
    "            if i in d:\n",
    "                d[i] += 1\n",
    "            else:\n",
    "                d[i] = 1\n",
    "        for i in guess:\n",
    "            if i in d and d[i] != 0:\n",
    "                d[i] -= 1\n",
    "                a1 += 1\n",
    "        for i,j in zip(solution,guess):\n",
    "            if i == j:\n",
    "                a0 += 1\n",
    "                a1 -= 1\n",
    "        return [a0,a1]\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        # 伪猜中 = guess 的字母在 solution 中总共出现的次数 - 猜中次数\n",
    "        count = collections.Counter(solution) \n",
    "        same, appear = 0, 0\n",
    "        for c1 in guess:\n",
    "            if count[c1] > 0:\n",
    "                appear += 1\n",
    "                count[c1] -= 1\n",
    "        \n",
    "        for c1, c2 in zip(guess, solution):\n",
    "            if c1 == c2:\n",
    "                same += 1 \n",
    "\n",
    "        return [same, appear - same]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        a=sum([i==j for i,j in zip(solution, guess)])\n",
    "        c=Counter(solution)\n",
    "        b=0\n",
    "        for i, num in enumerate(guess):\n",
    "            if num in c and c[num]>0:\n",
    "                c[num]-=1\n",
    "                b+=1\n",
    "        return [a, b-a]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        answer = [0 for i in range(2)]\n",
    "        svisited = [False for i in range(4)]\n",
    "        gvisited = [False for i in range(4)]\n",
    "        for i in range(4):\n",
    "            if solution[i] == guess[i]:\n",
    "                answer[0] += 1\n",
    "                svisited[i] = True\n",
    "                gvisited[i] = True\n",
    "        for i in range(4):\n",
    "            if gvisited[i] == False and guess[i] in solution:\n",
    "                gvisited[i] = True\n",
    "                for j in range(4):\n",
    "                    if guess[i] == solution[j] and svisited[j] == False:\n",
    "                        answer[1] += 1\n",
    "                        svisited[j] = True\n",
    "                        break\n",
    "        \n",
    "\n",
    "        return answer  \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        answer=[0,0]\n",
    "        for i,j in zip(solution,guess):\n",
    "            if i==j:\n",
    "                answer[0]+=1\n",
    "        g=list(guess)\n",
    "        for i in solution:\n",
    "            if i in g:\n",
    "                answer[1]+=1\n",
    "                g.remove(i)\n",
    "        answer[1]=answer[1]-answer[0]\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        cnt = dict(zip('RGBY', [[0, 0] for _ in range(4)]))\n",
    "        # print(cnt)\n",
    "        ans = [0] * 2\n",
    "        for i in range(len(solution)):\n",
    "            if solution[i] == guess[i]:\n",
    "                ans[0] += 1 \n",
    "            else:\n",
    "                cnt[solution[i]][0] += 1\n",
    "                cnt[guess[i]][1] += 1 \n",
    "            \n",
    "        print(cnt)\n",
    "        for v in cnt.values():\n",
    "            if v[0] > 0:\n",
    "                ans[1] += min(v)\n",
    "        \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        ans1,ans2=0,0\n",
    "        d1,d2={},{}\n",
    "        for c1,c2 in zip(solution,guess):\n",
    "            if c1==c2:\n",
    "                ans1+=1\n",
    "            else:\n",
    "                d1[c1]=d1.get(c1,0)+1\n",
    "                d2[c2]=d2.get(c2,0)+1\n",
    "        \n",
    "        for k in d1.keys():\n",
    "            ans2+=min(d1[k],d2.get(k,0))\n",
    "        return [ans1,ans2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        ftrue=min(solution.count('R'),guess.count('R'))+min(solution.count('Y'),guess.count('Y'))+min(solution.count('G'),guess.count('G'))+min(solution.count('B'),guess.count('B'))\n",
    "        true=0\n",
    "        for i in range(4):\n",
    "            if solution[i]==guess[i]:\n",
    "                true+=1\n",
    "        ftrue-=true\n",
    "        return[true,ftrue]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        fake_map=defaultdict(int)\n",
    "        fake_guess=[]\n",
    "        real=0\n",
    "        fake=0\n",
    "        for i in range(len(guess)):\n",
    "            if solution[i]==guess[i]:\n",
    "                real+=1\n",
    "            else:\n",
    "                fake_map[solution[i]]+=1\n",
    "                fake_guess.append(guess[i])\n",
    "        for fg in fake_guess:\n",
    "            if fg in fake_map and fake_map[fg]!=0:\n",
    "                fake+=1\n",
    "                fake_map[fg]-=1\n",
    "\n",
    "        return [real,fake]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        ans = [0,0]\n",
    "        r = 0\n",
    "        ress = []\n",
    "        resg = []\n",
    "        for i in range(4):\n",
    "            if solution[i] == guess[i]:\n",
    "                r += 1\n",
    "            else:\n",
    "                ress.append(solution[i])\n",
    "                resg.append(guess[i])\n",
    "        l = 0\n",
    "        for i in ress:\n",
    "            if i in resg:\n",
    "                l += 1\n",
    "                resg.remove(i)\n",
    "        return [r,l]\n",
    "            \n",
    "        \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        l1 = Counter(solution)\n",
    "        l2 = Counter(guess)\n",
    "        res1 = 0\n",
    "        res2 = 0\n",
    "        for i in range(4):\n",
    "            if guess[i] == solution[i]:\n",
    "                res1 += 1\n",
    "        for value in l1.keys():\n",
    "            res2 += min(l1[value], l2[value])\n",
    "\n",
    "        return [res1, res2 - res1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        s,g=defaultdict(int),defaultdict(int)\n",
    "        a=b=0\n",
    "        for i,j in zip(solution,guess):\n",
    "            if i==j:\n",
    "                a+=1\n",
    "            else:\n",
    "                s[i]+=1\n",
    "                g[j]+=1\n",
    "        for k,v in g.items():\n",
    "            if k in s:\n",
    "                b+=min(v,s[k])\n",
    "        return [a,b]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        ans = [0,0]\n",
    "        val = collections.defaultdict(int)\n",
    "        valguess = \"\"\n",
    "        for i in range(4):\n",
    "            if solution[i]==guess[i]:\n",
    "                ans[0]+=1\n",
    "            else:\n",
    "                val[solution[i]]+=1\n",
    "                valguess+=guess[i]\n",
    "        print(val,valguess)\n",
    "        for c in valguess:\n",
    "            if val[c]>0:\n",
    "                val[c]-=1\n",
    "                ans[1]+=1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        solution_count = {}\n",
    "        for si in solution:\n",
    "            if si not in solution_count:\n",
    "                solution_count[si] = 0\n",
    "            solution_count[si] += 1\n",
    "        res = [0, 0]\n",
    "        for si, gi in zip(solution, guess):\n",
    "            if si == gi:\n",
    "                res[0] += 1\n",
    "                solution_count[si] -= 1\n",
    "        \n",
    "        for si, gi in zip(solution, guess):\n",
    "            if gi != si and solution_count.get(gi, -1) > 0:\n",
    "                res[1] += 1\n",
    "                solution_count[gi] -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        a = 0\n",
    "        b = 0\n",
    "        counter = Counter()\n",
    "        pending = []\n",
    "\n",
    "        for right, a_guess in zip(solution, guess):\n",
    "            if right == a_guess:\n",
    "                a += 1\n",
    "\n",
    "            else:\n",
    "                counter[right] += 1\n",
    "                pending.append(a_guess)\n",
    "        \n",
    "        for a_guess in pending:\n",
    "            if counter[a_guess] > 0:\n",
    "                b += 1\n",
    "                counter[a_guess] -= 1\n",
    "        \n",
    "        return [a, b]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        guess_set = set(guess)\n",
    "        res = [0,0]\n",
    "        guess_full = 0\n",
    "        guess_half = 0\n",
    "        for i in guess_set:\n",
    "            if i in solution:\n",
    "                guess_half_i = min(guess.count(i),solution.count(i))\n",
    "                guess_half += guess_half_i\n",
    "        for j in range(len(solution)):\n",
    "            if solution[j] == guess[j]:\n",
    "                guess_full += 1\n",
    "        guess_half = guess_half - guess_full\n",
    "        res[0] = guess_full\n",
    "        res[1] = guess_half\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        s_map = collections.Counter(solution)\n",
    "        g_map = collections.Counter(guess)\n",
    "        guess_ture = sum(x == y for x, y in zip(solution, guess))\n",
    "        guess_other = sum(min(s_map[key], g_map[key]) for key in s_map.keys() & g_map.keys())\n",
    "        return [guess_ture, guess_other-guess_ture]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        s1,g1,n1 = [],[],0\n",
    "        for idx in range(len(solution)):\n",
    "            if solution[idx] != guess[idx]:\n",
    "                s1.append(solution[idx])\n",
    "                g1.append(guess[idx])\n",
    "            else:\n",
    "                n1 += 1\n",
    "        dss,dsg,n2 = Counter(s1),Counter(g1),0\n",
    "        for k in dss:\n",
    "            if k in dsg:\n",
    "                n2 += min(dss[k],dsg[k])\n",
    "        return [n1,n2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        dicts = collections.defaultdict(int)\n",
    "        dictg = collections.defaultdict(int)\n",
    "        hit = 0\n",
    "        pseudo_hit = 0\n",
    "        for ind in range(4):\n",
    "            if solution[ind] == guess[ind]:\n",
    "                hit += 1\n",
    "            else:\n",
    "                dicts[solution[ind]] += 1\n",
    "                dictg[guess[ind]] += 1\n",
    "        for slot in ['R', 'Y', 'G', 'B']:\n",
    "            pseudo_hit += min(dicts[slot], dictg[slot])\n",
    "        return [hit, pseudo_hit]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        # \"GGBB\" G[0 1]  B[2 3]\n",
    "        # \"RBYB\" R[0] Y[2] B[1 3]\n",
    "        # [1 1]\n",
    "        # \"RGRB\" R[0 2] G[1] B[3]\n",
    "        # \"BBBY\"             B[0 1 2]\n",
    "        # \"GBRB\" R[2] G[0] B[1 3]\n",
    "        ans = [0, 0]\n",
    "        for c1, c2 in zip(solution, guess):\n",
    "            if c1 == c2: ans[0] += 1\n",
    "        s_map = collections.defaultdict(list)\n",
    "        g_map = collections.defaultdict(list)\n",
    "        for i, (c1, c2) in enumerate(zip(solution, guess)):\n",
    "            s_map[c1].append(i)\n",
    "            g_map[c2].append(i)\n",
    "        for c in 'RYGB':\n",
    "            for i in range(4):\n",
    "                if i in s_map[c] and i in g_map[c]:\n",
    "                    s_map[c].remove(i)\n",
    "                    g_map[c].remove(i)\n",
    "            if len(s_map[c]) > 0 and len(g_map[c]) > 0:\n",
    "                ans[1] += min(len(s_map[c]), len(g_map[c]))\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        ans = [0,0]\n",
    "        val = collections.defaultdict(int)\n",
    "        valguess = \"\"\n",
    "        for i in range(4):\n",
    "            if solution[i]==guess[i]:\n",
    "                ans[0]+=1\n",
    "            else:\n",
    "                val[solution[i]]+=1\n",
    "                valguess+=guess[i]\n",
    "        for c in valguess:\n",
    "            if val[c]>0:\n",
    "                val[c]-=1\n",
    "                ans[1]+=1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        cnt = dict(zip('RGBY', [[0, 0] for _ in range(4)]))\n",
    "        # print(cnt)\n",
    "        ans = [0] * 2\n",
    "        for i in range(len(solution)):\n",
    "            if solution[i] == guess[i]:\n",
    "                ans[0] += 1 \n",
    "            else:\n",
    "                cnt[solution[i]][0] += 1\n",
    "                cnt[guess[i]][1] += 1 \n",
    "            \n",
    "        print(cnt)\n",
    "        for v in cnt.values():\n",
    "            if v[0] > 0:\n",
    "                ans[1] += min(v)\n",
    "        \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        temp = collections.Counter(solution)\n",
    "        ans1 = 0\n",
    "        ans2 = 0\n",
    "        \n",
    "        for i in range(len(guess)):\n",
    "            if guess[i] in temp.keys():\n",
    "                if temp[guess[i]] > 0:\n",
    "                    temp[guess[i]] -= 1\n",
    "                    ans1 += 1\n",
    "            if guess[i] == solution[i]:\n",
    "                ans2 += 1\n",
    "        \n",
    "\n",
    "        \n",
    "        return [ans2, ans1 - ans2]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        a = sum(i==j for i,j in zip(solution,guess))\n",
    "        b = sum((collections.Counter(solution) & collections.Counter(guess)).values())\n",
    "        return [a,b-a]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        r=0\n",
    "        for i,j in zip(solution,guess):\n",
    "            if i==j:\n",
    "                r+=1\n",
    "        z=0\n",
    "        g=list(guess)\n",
    "        for s in solution:\n",
    "            if s in g:\n",
    "                z+=1\n",
    "                g.remove(s)\n",
    "        return [r,z-r]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        s_count, g_count = Counter(solution), Counter(guess)\n",
    "        right = sum(x == y for x, y in zip(solution, guess))\n",
    "        not_bad = sum(min(s_count[key], g_count[key]) for key in set(solution) & set(guess))\n",
    "        return [right, not_bad - right]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        ans = [0,0]\n",
    "        hash_s = {}\n",
    "        hash_g = {}\n",
    "\n",
    "        for i in range(len(solution)):\n",
    "            hash_s[solution[i]] = hash_s.get(solution[i],0) + 1\n",
    "            hash_g[guess[i]] = hash_g.get(guess[i],0) + 1\n",
    "        \n",
    "        for i in range(len(guess)):\n",
    "            if guess[i] == solution[i]:\n",
    "                ans[0] += 1\n",
    "                hash_g[guess[i]] -= 1\n",
    "                hash_s[solution[i]] -= 1\n",
    "        \n",
    "        for key in hash_g.keys():\n",
    "            if hash_s.get(key) is not None and hash_g[key] > 0:\n",
    "                ans[1] += min(hash_g[key],hash_s[key])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "\"RGRB\"\n",
    "\"BBBY\"\n",
    "\"RGBY\"\n",
    "\"GGRR\"\n",
    "\n",
    "len(solution) = len(guess) = 4\n",
    "There are only \"R\",\"G\",\"B\",\"Y\" in solution and guess.\n",
    "\n",
    "Hash Table, String, Counting\n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        solReqFreq = Counter(solution)\n",
    "        hits = 0; total = 0\n",
    "        for i in range(len(guess)):\n",
    "            if solReqFreq[guess[i]] > 0:\n",
    "                total += 1\n",
    "                solReqFreq[guess[i]] -= 1\n",
    "            if guess[i] == solution[i]:\n",
    "                hits += 1\n",
    "        return [hits, total-hits]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        real,fake=0,0\n",
    "        lis=[0]*26\n",
    "        for i in range(4):\n",
    "            sol,gue=solution[i],guess[i]\n",
    "            if sol==gue:\n",
    "                real+=1\n",
    "            else:\n",
    "                if lis[ord(sol)-ord(\"A\")]<0:\n",
    "                    fake+=1\n",
    "                lis[ord(sol)-ord(\"A\")]+=1\n",
    "                if lis[ord(gue)-ord(\"A\")]>0:\n",
    "                    fake+=1\n",
    "                lis[ord(gue)-ord(\"A\")]-=1\n",
    "        return [real,fake]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        s_map, g_map = Counter(solution), Counter(guess)\n",
    "        guess_right = sum(x==y for x,y in zip(solution,guess))\n",
    "        guess_other = sum(min(s_map[key], g_map[key])  for key in set(solution) & set(guess))\n",
    "        return [guess_right, guess_other-guess_right]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        all_true,half_true = 0,0\n",
    "        for x,y in zip(solution,guess):\n",
    "            if x == y:\n",
    "                all_true += 1\n",
    "                solution = solution.replace(x,' ',1)\n",
    "                guess = guess.replace(y,'T',1)\n",
    "        for y in guess:\n",
    "            if y in solution:\n",
    "                half_true += 1\n",
    "                solution = solution.replace(y,' ',1)\n",
    "        return [all_true,half_true]\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        cnt=Counter(solution)\n",
    "        res=[0]*2\n",
    "        for i in range(4):\n",
    "            if solution[i]==guess[i]:\n",
    "                res[0]+=1\n",
    "                cnt[solution[i]]-=1\n",
    "            # else:\n",
    "        for i in range(4):\n",
    "            if solution[i]!=guess[i] and cnt[guess[i]]>0:\n",
    "                res[1]+=1\n",
    "                cnt[guess[i]]-=1\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        s = list(solution)\n",
    "        res = [0,0]\n",
    "        for x,y in enumerate(guess):\n",
    "            if y == solution[x]:\n",
    "                res[0] += 1\n",
    "                if y in s:\n",
    "                    s.remove(y)\n",
    "                else:\n",
    "                    res[1] -= 1\n",
    "            elif y in s:\n",
    "                res[1] += 1\n",
    "                s.remove(y)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        temp = collections.Counter(solution)\n",
    "        ans1 = 0\n",
    "        ans2 = 0\n",
    "        \n",
    "        for i in range(len(guess)):\n",
    "            if guess[i] in temp.keys():\n",
    "                if temp[guess[i]] > 0:\n",
    "                    temp[guess[i]] -= 1\n",
    "                    ans1 += 1\n",
    "            if guess[i] == solution[i]:\n",
    "                ans2 += 1\n",
    "        \n",
    "\n",
    "        \n",
    "        return [ans2, ans1 - ans2]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        a = sum(i == j for i, j in zip(solution, guess))\n",
    "        b = sum((collections.Counter(solution) & collections.Counter(guess)).values())\n",
    "        return [a, b - a]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        cnt = dict(zip('RGBY', [[0, 0] for _ in range(4)]))\n",
    "        # print(cnt)\n",
    "        ans = [0] * 2\n",
    "        for i in range(len(solution)):\n",
    "            if solution[i] == guess[i]:\n",
    "                ans[0] += 1 \n",
    "            else:\n",
    "                cnt[solution[i]][0] += 1\n",
    "                cnt[guess[i]][1] += 1 \n",
    "            \n",
    "        print(cnt)\n",
    "        for v in cnt.values():\n",
    "            if v[0] > 0:\n",
    "                ans[1] += min(v)\n",
    "        \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        import collections\n",
    "\n",
    "        hs = collections.Counter(solution)\n",
    "\n",
    "        res0 = 0\n",
    "        res1 = 0\n",
    "\n",
    "        temp = [] # 记录使用过的位置\n",
    "        for i in range(len(solution)):\n",
    "            if solution[i]==guess[i]:\n",
    "                res0+=1\n",
    "                hs[solution[i]]-=1\n",
    "                temp.append(i)\n",
    "\n",
    "        for i in range(len(solution)):\n",
    "            if guess[i] in hs and hs[guess[i]]>0 and i not in temp:\n",
    "                hs[guess[i]] -=1\n",
    "                res1 +=1\n",
    "          \n",
    "        return [res0,res1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        answer_0 = 0\n",
    "        answer_1 = 0\n",
    "        d = {}\n",
    "        \n",
    "        for i in solution:\n",
    "            if i in d:\n",
    "                d[i] += 1\n",
    "            else:\n",
    "                d[i] = 1\n",
    "        \n",
    "        for i in guess:\n",
    "            if i in d and d[i] != 0:\n",
    "                d[i] -= 1\n",
    "                answer_1 += 1\n",
    "\n",
    "        for i,j in zip(solution, guess):\n",
    "            if i == j:\n",
    "                answer_0 += 1\n",
    "                answer_1 -= 1\n",
    "        \n",
    "        return [answer_0, answer_1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        guessY = {\"R\":0,\"Y\":0,\"G\":0,\"B\":0}\n",
    "        guessN = {\"R\":0,\"Y\":0,\"G\":0,\"B\":0}\n",
    "        guessNumber = 0\n",
    "        nGuessNumber = 0\n",
    "        for i in range(len(solution)):\n",
    "            if solution[i] == guess[i]:\n",
    "                guessNumber += 1\n",
    "            else:\n",
    "                guessY[solution[i]] = guessY[solution[i]] + 1\n",
    "                guessN[guess[i]] = guessN[guess[i]] + 1\n",
    "        for i in (\"R\",\"Y\",\"G\",\"B\"):\n",
    "            nGuessNumber += min(guessY[i],guessN[i])\n",
    "        return [guessNumber,nGuessNumber]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def masterMind(self, solution: str, guess: str) -> List[int]:\n",
    "        total = sum((Counter(solution)&Counter(guess)).values())\n",
    "        right = sum(1 for (i,j) in zip(solution,guess) if i==j)\n",
    "        return [right,total-right]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
